/*
 * @Author: liuying 247220246@qq.com
 * @Date: 2025-02-28 13:58:44
 * @LastEditors: liuying 247220246@qq.com
 * @LastEditTime: 2025-03-04 11:21:44
 * @FilePath: \my-book-react\src\utils\url.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { parse, stringify } from 'query-string';

export const setUrlParams = (params: [string, string][], basePath = '/') => {
  if (!Array.isArray(params) || params.length === 0) return;

  const qs = parse(window.location.search);

  params.forEach((param) => {
    qs[param[0]] = param[1];
  });
  console.log(qs);

  window.history.replaceState(null, '', `${basePath}?${stringify(qs)}`);
};

export const removeUrlParams = (params: string[], basePath = '/') => {
  if (!Array.isArray(params) || params.length === 0) return;

  const qs = parse(window.location.search);

  params.forEach((param) => {
    delete qs[param];
  });

  const str = stringify(qs);

  window.history.replaceState(null, '', str ? `${basePath}?${str}` : basePath);
};
